home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 1 / Cream of the Crop 1.iso / PROGRAM / DJLSR106.ARJ / CRLF2NL.C < prev    next >
C/C++ Source or Header  |  1992-03-02  |  2KB  |  70 lines

  1. /* This is file CRLF2NL.C */
  2. /*
  3. ** Copyright (C) 1991 DJ Delorie, 24 Kirsten Ave, Rochester NH 03867-2954
  4. **
  5. ** This file is distributed under the terms listed in the document
  6. ** "copying.dj", available from DJ Delorie at the address above.
  7. ** A copy of "copying.dj" should accompany this file; if not, a copy
  8. ** should be available from where this file was obtained.  This file
  9. ** may not be distributed without a verbatim copy of "copying.dj".
  10. **
  11. ** This file is distributed WITHOUT ANY WARRANTY; without even the implied
  12. ** warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  13. */
  14.  
  15. unsigned crlf2nl(char *buf, unsigned len)
  16. {
  17.   char *bp = buf;
  18.   int i=0;
  19.   while (len--)
  20.   {
  21.     if (*bp != 13)
  22.     {
  23.       *buf++ = *bp;
  24.       i++;
  25.     }
  26.     bp++;
  27.   }
  28.   return i;
  29. }
  30.  
  31. unsigned readcr(int fd, char *buf, unsigned len)
  32. {
  33.   unsigned i;
  34.   i = read(fd, buf, len);
  35.   if (i <= 0)
  36.     return i;
  37.   return crlf2nl(buf, i);
  38. }
  39.  
  40. static char *sbuf = 0;
  41. #define BUFSIZE 4096
  42.  
  43. unsigned writecr(int fd, char *buf, unsigned len)
  44. {
  45.   unsigned bufp=0, sbufp=0, crcnt=0, rlen=0;
  46.   int rv;
  47.   if (sbuf == 0)
  48.     sbuf = (char *)malloc(BUFSIZE+1);
  49.   while (len--)
  50.   {
  51.     if (buf[bufp] == 10)
  52.     {
  53.       crcnt++;
  54.       sbuf[sbufp++] = 13;
  55.     }
  56.     sbuf[sbufp++] = buf[bufp++];
  57.     if ((sbufp >= BUFSIZE) || (len == 0))
  58.     {
  59.       rv = write(fd, sbuf, sbufp);
  60.       if (rv < 0)
  61.         return rv;
  62.       rlen += rv - crcnt;
  63.       crcnt = 0;
  64.       sbufp = 0;
  65.     }
  66.   }
  67.   return rlen;
  68. }
  69.  
  70.